﻿Imports SpatialAlbum.WPF.DundasMapVeneer.EnumExtent
Imports Dundas.Maps.WinControl


Partial Public Class ImageLoaderPage
    Inherits System.Windows.Controls.Page

    Private WithEvents m_job As ImageJob
    Private m_map As DundasMapVeneer = Nothing

    Const STEP_COUNT As Integer = 7 ' number of steps (or events) per image

    Public Sub New()
        InitializeComponent()
        MyInitialize()
    End Sub

    Private Sub MyInitialize()
        AddHandler g_imageloader.StartLoadingImage, AddressOf StartLoadingImage
        AddHandler g_imageloader.ImageLoaded, AddressOf ImageLoaded
    End Sub

    Private Sub ImageLoaderPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Loaded
        barJob.Maximum = STEP_COUNT
        barJob.Value = 0
        barOverall.Value = 0
        txtStatus.Text = "You can drag and drop jpeg files onto this window to load them into the database."
        txtStatus.Select(0, 0)

        If m_map Is Nothing Then
            m_map = New DundasMapVeneer(WindowsFormsHost1.Child)
            m_map.LoadMap(False)
            m_map.LoadTimeZones(False, False)
        End If

        chkTimeZones.IsChecked = False
        rdoWorld.IsChecked = True
    End Sub

    Private Delegate Sub DlgJobEvent(ByVal job As ImageJob)
    Private Sub StartLoadingImage(ByVal job As ImageJob)
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgJobEvent(AddressOf StartLoadingImage), job)
        Else
            m_job = job
            barJob.Value = 0
            Dim done As Integer = g_imageloader.DoneCount
            Dim queued As Integer = g_imageloader.QueueCount
            txtStatus.Text = (done + 1) & "/" & (queued + done + 1) & " - image '" & m_job.Name & "' "
            barOverall.Value = done
            barOverall.Maximum = queued + done + 1
        End If
    End Sub

    Private Sub ImageLoaded(ByVal job As ImageJob)
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgJobEvent(AddressOf ImageLoaded), job)
        Else
            barOverall.Value += 1
            If barOverall.Value = barOverall.Maximum Then
                txtStatus.Text = txtStatus.Text & vbCrLf & "loaded " & barOverall.Maximum & " image(s)"
                m_map.ClearHighlights()
            End If
            m_job = Nothing
        End If
    End Sub

    Private Sub m_job_Started() Handles m_job.Started
        UpdateProgress("", True)
    End Sub

    Private Sub m_job_InsertedRow(ByVal thumb As BitmapImage) Handles m_job.InsertedRow
        If thumb Is Nothing Then
            UpdateProgress("- inserted row (no thumbnail)", True)
        Else
            UpdateProgress("- inserted row", True)
        End If
    End Sub

    Private Sub m_job_Filestream() Handles m_job.Filestream
        UpdateProgress("- filestream", True)
    End Sub

    Private Delegate Sub DlgOnLocation(ByVal point As LatLong)
    Private Sub m_job_Location(ByVal point As LatLong) Handles m_job.Location
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgOnLocation(AddressOf m_job_Location), point)
        Else
            If point.IsNull Then
                UpdateProgress("- location (not found)", False)
            Else
                UpdateProgress("- location", False)
            End If
            If point.IsNull Then Return
            m_map.AddPhoto(point, True)
        End If
    End Sub

    Private Sub m_job_Metadata() Handles m_job.Metadata
        UpdateProgress("- metadata", True)
    End Sub

    Private Delegate Sub DlgTimeZone(ByVal tz As SpatialAlbum.Core.TimeZone)
    Private Sub m_job_TimeZone(ByVal tz As SpatialAlbum.Core.TimeZone) Handles m_job.TimeZoneFound
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgTimeZone(AddressOf m_job_TimeZone), tz)
        Else
            If tz Is Nothing Then
                UpdateProgress("- time zone (not found)")
                barJob.Value += 1 ' because the next event for the datetimeoffset won't be fired
            Else
                UpdateProgress("- time zone")
            End If
            If tz IsNot Nothing Then
                Dim dundasmap As MapControl = CType(WindowsFormsHost1.Child, MapControl)
                Dim elements As ArrayList = dundasmap.Find(tz.UniqueID.ToString, False, True)
                For Each e As Object In elements
                    If TypeOf e Is Shape Then m_map.FlashShape(CType(e, Shape))
                Next
            End If
        End If
    End Sub

    Private Sub m_job_Completed(ByVal fname As String, ByVal loc As LatLong, ByVal thumbnail As System.Windows.Media.Imaging.BitmapImage) Handles m_job.Completed
        UpdateProgress("loaded image " & m_job.Name, True)
    End Sub

    Private Sub m_job_ExceptionOccurred(ByVal ex As System.Exception) Handles m_job.ExceptionOccurred
        UpdateProgress("exception: " & ex.Message, True)
    End Sub

    Private Delegate Sub DlgUpdateProgress(ByVal msg As String, ByVal doevents As Boolean)
    Private Sub UpdateProgress(Optional ByVal msg As String = "", Optional ByVal doevents As Boolean = False)
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgUpdateProgress(AddressOf UpdateProgress), msg, doevents)
        Else
            If msg <> "" Then
                If txtStatus.Text = "" Then
                    txtStatus.Text = msg
                Else
                    txtStatus.AppendText(vbCrLf & msg)
                End If
            End If
            barJob.Value += 1
            If doevents Then
                'Me.Update()
                'My.Application.DoEvents()
            End If
        End If
    End Sub

    Private Sub SetMapExtent() Handles rdoWorld.Checked, rdoCONUS.Checked, rdoNorthAmerica.Checked
        If rdoWorld.IsChecked Then m_map.SetMapExtent(eWorld)
        If rdoCONUS.IsChecked Then m_map.SetMapExtent(eCONUS)
        If rdoNorthAmerica.IsChecked Then m_map.SetMapExtent(eNorthAmerica)
    End Sub

    Private Sub chkTimeZones_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTimeZones.Checked, chkTimeZones.Unchecked
        m_map.ClearLayer("TimeZone")
        m_map.LoadTimeZones(chkTimeZones.IsChecked, False)
    End Sub


    Private Sub btnLoadImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadImage.Click
        If My.Computer.Keyboard.ShiftKeyDown Then
            LoadChinaBugPhoto()
            Return
        End If
        g_imageloader.AddImage(IO.Path.Combine(IO.Directory.GetCurrentDirectory(), My.Settings.RTS_TestPhoto3), g_connection)
    End Sub

    Private Sub LoadChinaBugPhoto()
        BugMode()
    End Sub

    Private Sub BugMode()
        m_map.LoadMap(True)
        m_map.LoadTimeZones(True, True)
    End Sub

End Class
